#include<iostream>
using namespace std;
#include<vector>
#include<queue>
class Solution {
public:
    int eatenApples(vector<int>& apples, vector<int>& days) {
        int arr[50000] = { 0 };
        priority_queue<int, vector<int>, greater<int>> que;
        int ans = 0;
        for (int i = 0; i < apples.size(); ++i) {
            if (days[i] > 0)
                arr[i + days[i] - 1] += apples[i];
            que.push(i + days[i] - 1);
            while (que.size() && que.top() < i )
                que.pop();
            if (que.size()) {
                --arr[que.top()];
                while (que.size()&&arr[que.top()] == 0)
                    que.pop();
                ++ans;
            }
        }
        int now = apples.size();
        while (que.size()) {
            while (que.size()&&que.top() < now)
                que.pop();
            if (que.size() == 0)
                break;
            --arr[que.top()];
            while (que.size() && arr[que.top()] == 0)
                que.pop();
            ++ans;
            ++now;
        }
        return ans;
    }
};
int main() {
    vector<int> arr{ 2,1,10}, arr2{ 2,10,1};
    Solution().eatenApples(arr, arr2);
}